package ControlLayer;

import java.util.ArrayList;
import DBLayer.DBSupplier;
import DBLayer.DBConnection;
import DBLayer.IFDBSupplier;
import ModelLayer.Supplier;
import ModelLayer.Product;

public class SupplierCtr 
{
public SupplierCtr(){
		
	}
	
	//List all suppliers
	public ArrayList<Supplier> listAllSupplier()
	{
		IFDBSupplier dbSup = new DBSupplier();
		ArrayList<Supplier> allSup = new ArrayList<Supplier>();
		allSup = dbSup.listAllSuppliers(false);
		return allSup;
	}
	
	//Finds a supplier on the entered supplier ID
	public Supplier findSupplier(int supplierId)
	{
		IFDBSupplier dbSup = new DBSupplier();
		return dbSup.findSupplier(supplierId, true);
	}
	
	//Updates a supplier object
	public int updateSupplier(int supplierId, String supplierName, String country, int phoneNumber, String email, ArrayList<Product> tire, ArrayList<Product> glass, ArrayList<Product> gearBox, ArrayList<Product> headLights, ArrayList<Product> brakeDisc)
	{
		IFDBSupplier dbSup = new DBSupplier();
		Supplier supObj = new Supplier();
		
		supObj.setSupplierId(supplierId);
		supObj.setSupplierName(supplierName);
		supObj.setCountry(country);
		supObj.setPhoneNumber(phoneNumber);
		supObj.addProductsArray(tire);
		supObj.addProductsArray(glass);
		supObj.addProductsArray(gearBox);
		supObj.addProductsArray(headLights);
		supObj.addProductsArray(brakeDisc);
		return dbSup.updateSupplier(supObj);
	}
	
	
	//Creates and inserts a new supplier in the database
	public void insertSupplier(int supplierId, String supplierName, String country, int phoneNumber, String email, ArrayList<Product> tire, ArrayList<Product> glass, ArrayList<Product> gearBox, ArrayList<Product> headLights, ArrayList<Product> brakeDisc)
	{
		Supplier supObj = new Supplier();
		supObj.setSupplierId(supplierId);
		supObj.setSupplierName(supplierName);
		supObj.setCountry(country);
		supObj.addProductsArray(tire);
		supObj.addProductsArray(glass);
		supObj.addProductsArray(gearBox);
		supObj.addProductsArray(headLights);
		supObj.addProductsArray(brakeDisc);
		
		try{
			DBConnection.startTransaction();
			DBSupplier dbSup = new DBSupplier();
			dbSup.insertSupplier(supObj);
			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//Deletes a supplier on supplier ID
	public void deleteSupplier(int supplierId)
	{
		DBSupplier dbSupplier = new DBSupplier();
		dbSupplier.deleteSupplier(supplierId);	
	}
	
	//Returns a suppler on its ID
	public String getSupplier(int supplierId)
	{
		DBSupplier dbSupplier = new DBSupplier();
		String supplier = dbSupplier.getSupplier(supplierId);
		return supplier;
	}

}
